<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  <link rel="icon" href="favicon.ico" />
  <title>Html - 实验室</title>
  <style>
    html,
    body {
      border: 0;
      padding: 0;
      margin: 0;
      height: 100%;
      overflow: hidden;
    }
  </style>
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
  <div id="app">

    <div>Html 实验室</div>
    <input id="username" type="password" placeholder="username" value="15290483314">
    <input id="password" type="password" placeholder="password" value="jia8598110">
    <button onclick="login()">登录</button>
    <input type="password" id="cookie" placeholder="cookie 需要复制登录的响应头" style="width:40%" >
    <!-- <button onclick="inputCookie()">设置Cookie</button><br> -->
    <textarea placeholder="返回结果" id="data" cols="30" rows="1" style="width:80%"></textarea><br>
    <el-select v-model="value" placeholder="请选择">
      <el-option label="全部" :value="null"></el-option>
      <el-option label="普通" :value="0"></el-option>
      <el-option label="奖励" :value="1"></el-option>
    </el-select>
    <el-select v-model="jgValue" filterable multiple placeholder="请选择机构">
      <template slot="append">.com</template>
      <el-option v-for="item in jgOptions" :key="item.corporationId" :label="item.corporationName"
        :value="item.corporationId">
      </el-option>
    </el-select>
    <button onclick="getJgList()">机构更新</button>
    <button onclick="sendDataList()">数据查询</button><br><br>
    <!-- 额外奖励 <input type="text" id="bs" value="2.5" style="width: 40px" />%
    <button onclick="search1()">查询</button> 2 ~ 4 之间 越小奖励越大<br>
    <textarea id="resText" cols="30" rows="10" style="width:80%"></textarea><br><br> -->

    活动<input type="text" id="hdlx" value="0" style="width: 40px" />%
    年化率 <input type="text" id="nhl" value="12" style="width: 40px" />%
    <input type="checkbox" id="isNhl" checked>是否去除机构0.15</input>
    <el-date-picker v-model="dateValue" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions">
    </el-date-picker>
    <button onclick="search2()">查询</button>
    (贴现[是否减 0.15] + 奖励 + 活动点) * 365 / 购入金额 / 贴现天数<br>

    <textarea id="resText2" cols="30" rows="30" style="width:80%"></textarea><br><br>

  </div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  const vue = new Vue({
    el: '#app',
    data: vm => ({
      value: 1,
      jgOptions: JSON.parse(localStorage.getItem("HXJ-jgOptions")),
      jgValue: [],
      pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '今年',
            onClick(picker) {
              var strTime= new Date().getFullYear()+"-01-01";    //字符串日期格式           
              var date= new Date(Date.parse(strTime.replace(/-/g,  "/"))); 
              picker.$emit('pick', date);
            }
          }, {
            text: '年+1',
            onClick(picker) {
              var strTime= (new Date().getFullYear() + 1)+"-01-01";    //字符串日期格式           
              var date= new Date(Date.parse(strTime.replace(/-/g,  "/"))); 
              picker.$emit('pick', date);
            }
          }, {
            text: '年+2',
            onClick(picker) {
              var strTime= (new Date().getFullYear() + 2)+"-01-01";    //字符串日期格式           
              var date= new Date(Date.parse(strTime.replace(/-/g,  "/"))); 
              picker.$emit('pick', date);
            }
          }, {
            text: '年+3',
            onClick(picker) {
              var strTime= (new Date().getFullYear() + 3)+"-01-01";    //字符串日期格式           
              var date= new Date(Date.parse(strTime.replace(/-/g,  "/"))); 
              picker.$emit('pick', date);
            }
          }]
        },
      dateValue: ''
    })
  })
  function login() {
    axios.post(`https://api.faig.top/sms/api?targetUrl=http://quanyi.hxjx360.com/api/login2&username=${$("#username").val()}&password=password_CODE_PW_OPENID_${$("#password").val()}_CODE_PW_OPENID_undefined`, {}, {
      // axios.get(`http://127.0.0.1:8080/api?targetUrl=https://api.faig.top/sms/loginUser&username=fhs&password=jia8598110`, {
      headers: {
        'g-set-cookie': true,
      }
    })
      .then(res => {
        $('#cookie').val(res.headers['g-set-cookie'])
        inputCookie()
      })
  }

  function getJgList() {
    let cookieValue = $("#cookie").val() || localStorage.getItem("HXJ-Cookie")
    axios.post(`https://api.faig.top/sms/api?targetUrl=http://quanyi.hxjx360.com/api/uiweb/equities/corporationList&type=EQUITIES`, {}, {
      // axios.get(`http://127.0.0.1:8080/api?targetUrl=https://api.faig.top/sms/loginUser&username=fhs&password=jia8598110`, {
      headers: {
        's-cookie': cookieValue
      }
    })
      .then(res => {
        vue._data.jgOptions = res.data.items
        localStorage.setItem("HXJ-jgOptions", JSON.stringify(res.data.items))
      })
  }

  function sendDataList() {
    $("#data").empty();
    console.log($("#cookie").val() || localStorage.getItem("HXJ-Cookie"));
    let cookieValue = $("#cookie").val() || localStorage.getItem("HXJ-Cookie")
    $.ajax({
      type: "POST",
      // url: "http://192.168.1.68:8080/api?targetUrl=http://quanyi.hxjx360.com/api/uiweb/equities/list",
      url: "https://api.faig.top/sms/api?targetUrl=http://quanyi.hxjx360.com/api/uiweb/equities/list",
      headers: {
        's-cookie': cookieValue
      },
      contentType: "application/json",
      xhrFields: { withCredentials: true },
      data: JSON.stringify({
        corporationIds: vue._data.jgValue,
        novice: false,
        page: 1,
        pageSize: 9999,
        sortType: 1,
        sortValue: "deadLine",
        type: vue._data.value,
      }),
      dataType: "json",
      success: function (data) {
        let dataList = JSON.stringify(data)
        localStorage.setItem("HXJ-dataList", dataList)
        console.log('设置 dataList', dataList);
        $("#data").val(dataList);
      },
    });
  }

  function getDateList() {
    let dataHtml = $("#data").val() || localStorage.getItem("HXJ-dataList")
    const data = JSON.parse(dataHtml)
    console.log('data', data);

    // 预计贴现时间 deadLine
    // 预计应收 paymentAmount
    // 预计贴现 expectedEarnings
    // 奖励 awardAmount
    // 购入金额 amount
    return data
  }

  /**
   * 额外奖励查询
   * */
  function search1() {
    const num = Number($("#bs").val());
    const data = getDateList()
    let list = data.items.data;
    let fList = list.filter((item) => {
      return item.expectedEarnings / item.awardAmount <= num;
    }).map(item => {
      let xcTime = getDaysBetween(item.calDate, item.deadLine)
      item.xcTime = xcTime
      return item;
    });
    var resTextHtml = "";
    $("#resText").empty();
    fList.forEach((element) => {
      console.log(
        `符合 ${num} 的机构`,
        element,
        element.corporationName,
        getDaysBetween(element.calDate, element.deadLine)
      );
      resTextHtml += `    机构: ${element.corporationName} - 借贷人: ${element.loanUserName} - 预计贴现 ${element.deadLine} - 应收 ${element.paymentAmount} - 购入 ${element.amount} - 预计贴现 ${element.expectedEarnings} - 奖励 ${element.awardAmount} - 相差 ${element.xcTime}
`
    });
    $("#resText").html(resTextHtml || '无');
  }


  /**
   * 年利率
   * (贴现[是否 * 0.15] + 奖励) * 365 / 购入金额 / 贴现天数
   * (预计贴现[是否 * 0.15] + 奖励) * 365 / 购入金额 / 贴现天数
   * */
  function search2() {
    const data = getDateList()
    let list = data.items.data;
    // 年化率
    let hdlx = Number($("#hdlx").val())
    let nhl = $("#nhl").val()
    let is = $("#isNhl")[0].checked

    let fList = list.filter(item => {
      var dateValue = vue._data.dateValue
      if(dateValue){
        var time = dateValue.getTime(); 
        var time2 = new Date(Date.parse(item.deadLine.substring(0, 10).replace(/-/g,  "/"))).getTime(); 
        return time2 >= time
      }
      return true
    }).filter((item) => {
      let expectedEarnings = item.expectedEarnings
      let jlAmount = item.amount * hdlx / 100
      if (is) expectedEarnings = item.expectedEarnings - (item.expectedEarnings * 0.15);
      let data = (expectedEarnings + item.awardAmount + jlAmount) * 365 / item.amount / getDaysBetween(item.calDate, item.deadLine)
      let nhlValue = (data * 100).toFixed(2)

      // console.log(Number(nhlValue), nhl, Number(nhlValue) > Number(nhl));
      return Number(nhlValue) > Number(nhl);
    }).map(item => {
      let expectedEarnings = item.expectedEarnings
      if (is) expectedEarnings = item.expectedEarnings - (item.expectedEarnings * 0.15);
      let jlAmount = item.amount * hdlx / 100
      let xcTime = getDaysBetween(item.calDate, item.deadLine)
      let data = (expectedEarnings + item.awardAmount + jlAmount) * 365 / item.amount / xcTime
      let nhlValue = Number((data * 100).toFixed(2))

      item.nhlValue = nhlValue
      item.xcTime = xcTime
      item.jlAmount = jlAmount
      item.expectedEarnings2 = expectedEarnings.toFixed(2)
      item.rx = ((Number(item.expectedEarnings2) + Number(item.awardAmount)) / Number(item.xcTime)).toFixed(6)
      return item;
    });

    let resTextHtml = ''
    $("#resText2").empty();
    fList.forEach((element) => {
      console.log(
        `符合 ${nhl} 的机构`,
        element,
        element.corporationName,
        getDaysBetween(element.calDate, element.deadLine)
      );
      resTextHtml += `    机构: ${element.corporationName} - 借贷人: ${element.loanUserName} - 预计贴现 ${element.deadLine} - 应收 ${element.paymentAmount} - 第${element.currentIssue}/${element.monthToReturn}期 - 购入 ${element.amount} - 预计贴现 ${element.expectedEarnings} - 真实贴现 ${element.expectedEarnings2} - 奖励 ${element.awardAmount} - 年化率 ${element.nhlValue} - 相差 ${element.xcTime} - 日息 ${element.rx} - 奖励 ${element.jlAmount}
`
    });
    $("#resText2").html(resTextHtml || '无');



  }
  /**
   * 获取两个日期之间的值 
   **/
  function getDaysBetween(dateString1, dateString2) {
    var startDate = Date.parse(dateString1);
    var endDate = Date.parse(dateString2);
    if (startDate > endDate) {
      return 0;
    }
    if (startDate == endDate) {
      return 1;
    }
    var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    return parseInt(days);
  }

  function inputCookie() {
    let cv = $("#cookie").val()
    if (cv) {
      localStorage.setItem("HXJ-Cookie", cv)
      console.log('设置 cookie', cv);
    }
  }
</script>

</html>